<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;
//测试控制器
class ListController extends CommonController{
    //构造函数
	public function __init()
	{
		
	}
    //动作
    public function index(){
    	$cid = Q("get.cate",0,"intval");
		$bid = Q("get.b",0,"intval");
		if($cid){
			$data = Db::table("360mall_category")->where("cate_pid",$cid)->get();	
		}else{
			$data = Db::table("360mall_category")->get();
		}
	
        //获得父类
        $fatherData = $this->getFather(Db::table("360mall_category")->get(),$cid);
		//数组反转
		$fatherData = array_reverse($fatherData);
        View::with("fatherData",$fatherData);
	   //找子类分类
	    $cids = $this->getSon(Db::table("360mall_category")->get(), $cid);
		$cids[] = $cid;
	   //在商品表中通过参类id查找出商品数据
	     $bids = Db::table("360mall_goods")->where("g_cid  in (".implode(",", $cids).")")->lists("g_bid");
	 //品牌信息
	   if($bids){
	 	
	 	$bidData  = Db::table("360mall_brand")->where("b_id  in (".implode(",",$bids).")")->get(); 
	    View::with("bidData",$bidData);
	   }
		
        if($cids&&$bid){
        	$catedata = Db::table("360mall_goods")->where("g_cid  in (".implode(",", $cids).")")->where("g_bid",$bid)->get();
        	
        }else{
        
        	$catedata = Db::table("360mall_goods")->where("g_cid  in (".implode(",", $cids).")")->get();
        }
		
        $gids = Db::table("360mall_goods")->where("g_cid  in (".implode(",", $cids).")")->lists("g_id");
        View::with("catedata",$catedata);
		View::with("data",$data);
        View::make();
     
    }
	//获得子类
	 public function getSon($data,$cate_id){
	  	//静态变量为了防止结果不被覆盖
		 static $temp = array();
		 foreach ($data as $key => $v) {
		 	//条件成立的时候，当前这一条就是$cate_id这一条的子集
		 	if($cate_id==$v['cate_pid']){
		 		$temp[] = $v['cate_id'];
				//递归找子集
				$this->getSon($data,$v['cate_id']);
			}
		}
		return $temp;
	  }
	//找父级
	 public function getFather($data,$cate_id){
	  	//静态变量为了防止结果不被覆盖
		 static $temp = array();
		 foreach ($data as $key => $v) {
		 	//条件成立的时候，当前这一条就是$cate_id这一条的子集
		 	if($cate_id==$v['cate_id']){
		 		$temp[] = $v;
				//递归找子集
				$this->getFather($data,$v['cate_pid']);
			}
		}
		return $temp;
	  }
	 
	 
	 
	 
	 
	 
	 
}
